import javax.swing.*;
import java.io.*;
import java.awt.event.*;
public class Main
{
	public static void main(String[] args)
	{
		new Main().run();
	}
	public void run() {
		JTextField textfield = new JTextField(9);
		JButton clsButton = new JButton("Search class");
		JButton pkgButton = new JButton("Search package");
		
		JPanel panel = new JPanel();
		JFrame frame = new Gooey(panel);
		
		panel.add(textfield);
		panel.add(clsButton);
		panel.add(pkgButton);
		
		frame.pack();
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		clsButton.addActionListener(new JButtonListener(textfield,"allclasses-noframe.html",1));
		pkgButton.addActionListener(new JButtonListener(textfield,"overview-frame.html",3));

		frame.getRootPane().setDefaultButton(clsButton);
	}
	class JButtonListener implements ActionListener
	{
		JTextField textfield;
		String fileName;
		int index;
		JButtonListener(JTextField input,String fileName,int arrayIndex)
		{
			textfield = input;
			this.fileName = fileName;
			index = arrayIndex;
		}
		Backend backend = new Backend();
		public void actionPerformed(ActionEvent e)
		{
			try
			{
				BufferedReader in = new BufferedReader(new FileReader(fileName));
				String line = in.readLine();
				
				String text = textfield.getText();
				if("".equals(text))
				{
					System.out.println("hello everyone");
					backend.command(
								"http://java.sun.com/javase/6/docs/api/");
				}
				else
				{
					while(line != null)
					{
						if(line.toLowerCase().contains("/" +text.toLowerCase()+ ".html"))
						{
							String[] sa = line.split("\"");
							System.out.println("hello world");
							backend.command(
									"http://java.sun.com/javase/6/docs/api/" + sa[index]);
							textfield.setText("");
							break;
						}
						line = in.readLine();
					}
				}
				
			}
			catch(FileNotFoundException ex)
			{
				ex.printStackTrace();
				System.out.println(ex);
			}
			catch(IOException ex)
			{
				System.out.println(ex);
			}
		}
	}
}